﻿using System;
using System.ComponentModel;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Win.Templates.ActionContainers;
using DevExpress.XtraBars;
using DevExpress.ExpressApp;
using Hdt.TransOcean.Dal;
using System.Windows.Forms;

namespace Hdt.TransOcean.Presentation
{
    public partial class FormJourney : DevExpress.ExpressApp.Win.Templates.XtraFormTemplateBase, ISupportClassicToRibbonTransform
    {
        private EOperation _operation;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CheckTransformToRibbon();
        }
        protected override IModelFormState GetFormStateNode()
        {
            if (View != null)
            {
                return TemplatesHelper.GetFormStateNode(View.Id);
            }
            else
            {
                return base.GetFormStateNode();
            }
        }
        public override void SetSettings(IModelTemplate modelTemplate)
        {
            base.SetSettings(modelTemplate);
            formStateModelSynchronizerComponent.Model = GetFormStateNode();
        }
        [Obsolete("Use the DetailViewForm() constructor"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public FormJourney(XafApplication application) : this() { }
        public FormJourney()
        {
            InitializeComponent();
        }
        public FormJourney(EOperation operation, string code = null)
        {
            InitializeComponent();
            this._operation = operation;
            //txtCode.Enabled = false;
            Presentation.BindGridLookupEdit(gridLookUpEditJourneyType, Presentation.GetData(EObject.JourneyType));
            Presentation.DisplayButtonByOperation(hdtManageControl1,operation);
            if (operation == EOperation.New)
            {
                txtCode.Enabled = true;
                gridLookUpEditJourneyType.EditValue = 1;
            }
                //txtCode.Text = Dao.GetAutoCode("Journey");
            if (operation == EOperation.Edit)
            {
                txtCode.Enabled = false;
                if (!String.IsNullOrEmpty(code))
                {
                    Journey journey = Dao.SelectJourney(code)[0];

                    txtCode.Text = journey.code;
                    txtName.Text = journey.name;
                    txtDescription.Text = journey.description;
                    gridLookUpEditJourneyType.EditValue = journey.type;
                    chkDeleted.EditValue = journey.deleted;
                }
            }
        }
        private void hdtManageControl1_HdtNewButtonClick()
        {
            ResetJourneyInfo();
            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.New);
        }

        private void ResetJourneyInfo()
        {
            txtCode.Text = String.Empty;
            txtName.Text = String.Empty;
            txtDescription.Text = String.Empty;
            chkDeleted.Enabled = false;
            gridLookUpEditJourneyType.EditValue = 1;
        }

        private void hdtManageControl1_HdtSaveButtonClick()
        {
            string message = "Lưu thành công!";
            //bool flag = true;
            Dao dao = new Dao();
            Journey journey = new Journey();
            journey.code = txtCode.Text;
            journey.name = txtName.Text;
            journey.type = (int)gridLookUpEditJourneyType.EditValue;
            journey.description = txtDescription.Text;
            journey.deleted = 0;

            try
            {
                if (Presentation.ValidJourney(journey) == true)
                {
                    if (this._operation == EOperation.Edit)
                    {
                        dao.UpdateJourney(journey);
                    }
                    else if (this._operation == EOperation.New)
                    {
                        Dao.InsertJourney(journey);
                        ResetJourneyInfo();
                    }
                    Presentation.DisplayButtonByOperation(hdtManageControl1,EOperation.Save);
                }
            }
            catch (Exception e)
            {
                if (e != null)
                {
                    //flag = false;
                    message = e.ToString();
                }
            }
            finally
            {
                MessageBox.Show(message, "Hành trình - Lưu");
            }
        }

        private void hdtManageControl1_HdtEditButtonClick()
        {
            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.Edit);
        }

        private void hdtManageControl1_HdtDeleteButtonClick()
        {
            if (!String.IsNullOrEmpty(txtCode.Text))
            {
                try
                {
                    if (MessageBox.Show("Bạn muốn xóa không?", "Hành trình", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                    {
                        if (Dao.DeleteJourneyByCode(txtCode.Text) == true)
                        {
                            MessageBox.Show("Xóa thành công", "Hành trình");
                            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.Delete);
                        }
                    }
                }
                catch (Exception e)
                {

                    MessageBox.Show(e.ToString());
                }
            }
        }

        private void hdtManageControl1_HdtCloseButtonClick()
        {
            this.Close();
        }

        private void hdtManageControl1_HdtExportButtonClick()
        {
            Dao.ExportExcel(EObject.Journey, "journey.xls", null);
        }

        private void hdtManageControl1_HdtImportButtonClick()
        {
            Dao.ImportExcel(EObject.Journey);
        }

        private void FormJourney_Load(object sender, EventArgs e)
        {
            
        }
    }
}
